home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / csim / source.lha / source / C++SIM / lwp_thread.cc < prev    next >
C/C++ Source or Header  |  1993-06-14  |  2KB  |  119 lines

  1. /*
  2.  * Copyright (C) 1993
  3.  *
  4.  * Department of Computing Science,
  5.  * The University,
  6.  * Newcastle upon Tyne,
  7.  * UK.
  8.  */
  9.  
  10. #ifndef COMMON_H_
  11. #include "common.h"
  12. #endif
  13.  
  14. #ifndef LWPTHREAD_H_
  15. #include "lwp_thread.h"
  16. #endif
  17.  
  18.  
  19. /* These are the Sun C routines which give access to threads. They have to be
  20.  * defined in this way. The multiple definitions for lwp_create are as a result
  21.  * of the way in which C++ allows C routines to be declared and the way in which
  22.  * Sun's lwp_create is defined.
  23.  */
  24.  
  25. extern "C"
  26. {
  27. #include <lwp/stackdep.h>
  28.  
  29.     int pod_setmaxpri(int);
  30.     int lwp_setstkcache(int, int);
  31. #ifndef Scheduler_
  32. #ifdef Main_
  33.     int lwp_create(thread_t*, void (*func)(), int, int, stkalign_t*, int);
  34. #else
  35.     int lwp_create(thread_t*, void (*func)(LWP_Thread *), int, int, stkalign_t*, int, caddr_t);
  36. #endif
  37. #endif
  38.     int lwp_yield(thread_t);
  39.     int lwp_suspend(thread_t);
  40.     int lwp_resume(thread_t);
  41.     int lwp_setpri(thread_t, int);
  42.     int lwp_self(thread_t*);
  43.     int lwp_ping(thread_t);
  44.     int lwp_sleep(struct timeval);
  45. }
  46.  
  47. //
  48. // Class LWP_Thread
  49. //
  50.  
  51. const int LWP_Thread::MaxPriority=10;
  52.  
  53. LWP_Thread::LWP_Thread (int prio)
  54. {
  55.     caddr_t p1;
  56.  
  57.     p1 = (caddr_t) this;
  58.     (void) lwp_create(&mid, LWP_Thread::Execute, prio, 0, lwp_newstk(), 1, p1);
  59.     thread_key = mid.thread_key;
  60.     (void) lwp_suspend(mid);
  61. }
  62.  
  63. // For creating the LWP_Thread for "main"
  64.  
  65. LWP_Thread::LWP_Thread (thread_t tid)
  66. {
  67.     thread_key = tid.thread_key;
  68. }
  69.  
  70. LWP_Thread::~LWP_Thread () {}
  71.  
  72. long LWP_Thread::Current_Thread () const
  73. {
  74.     thread_t tid;
  75.  
  76.     (void) lwp_self(&tid);
  77.  
  78.     return tid.thread_key;
  79. }
  80.  
  81. void LWP_Thread::Execute (LWP_Thread *p1) { p1->Body(); }
  82.  
  83. void LWP_Thread::Suspend () { (void) lwp_suspend(mid); }
  84.  
  85. void LWP_Thread::Resume () { (void) lwp_resume(mid); }
  86.  
  87. void LWP_Thread::Sleep (struct timeval doze) { (void) lwp_sleep(doze); }
  88.  
  89. thread_t LWP_Thread::Thread_ID () const { return mid; }
  90.  
  91.  
  92.  
  93. //
  94. // Getting the main thread into the thread list...
  95. //
  96.  
  97. class LWP_Main_Thread : public LWP_Thread
  98. {
  99. public:
  100.     LWP_Main_Thread (thread_t t) : LWP_Thread(t) {}
  101.     ~LWP_Main_Thread ();
  102.  
  103.     void Body ();
  104. };
  105.  
  106. LWP_Main_Thread::~LWP_Main_Thread () {}
  107.  
  108. void LWP_Main_Thread::Body () {}
  109.  
  110. void LWP_Thread::Initialize ()
  111. {
  112.     (void) pod_setmaxpri(MaxPriority);
  113.     (void) lwp_setstkcache(4000, 40);
  114.  
  115.     thread_t me;
  116.     lwp_self(&me);
  117.     new LWP_Main_Thread(me);
  118. }
  119.